package com.bingxue.edu.parent.service;

import com.bingxue.edu.common.enums.ChangeTypeEnum;
import com.bingxue.edu.common.enums.ClassStatusEnum;
import com.bingxue.edu.common.enums.StudentClassEnum;
import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.management.education.model.query.EnrollClassQuery;
import com.bingxue.edu.management.education.model.req.ClassChangeReq;
import com.bingxue.edu.management.education.service.ClassChangeService;
import com.bingxue.edu.management.education.service.ClassStudentService;
import com.bingxue.edu.management.education.service.EnrollChangeRecordService;
import com.bingxue.edu.management.education.service.EnrollmentService;
import com.bingxue.edu.management.education.service.LessonService;
import com.bingxue.edu.parent.model.query.ParentStudentClassQuery;
import com.bingxue.edu.parent.model.query.StudentClassChangeRecordQuery;
import com.bingxue.edu.parent.model.resp.StudentClassChangeRecordResp;
import com.bingxue.edu.parent.model.resp.StudentClassInfoResp;
import com.bingxue.edu.parent.model.resp.StudentClassLessonResp;
import com.bingxue.edu.parent.model.resp.StudentEnrollClassResp;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.ClassInfoTableDef.CLASS_INFO;
import static com.bingxue.edu.management.education.model.entity.table.ClassStudentTableDef.CLASS_STUDENT;
import static com.bingxue.edu.management.education.model.entity.table.EnrollChangeRecordTableDef.ENROLL_CHANGE_RECORD;
import static com.bingxue.edu.management.education.model.entity.table.LessonTableDef.LESSON;

@Slf4j
@Service
@RequiredArgsConstructor
public class StudentClassService {

    private final EnrollmentService enrollmentService;
    private final ClassStudentService classStudentService;
    private final ClassChangeService classChangeService;
    private final LessonService lessonService;
    private final EnrollChangeRecordService enrollChangeRecordService;

    public Pagination<StudentEnrollClassResp> getEnrollClassPage(EnrollClassQuery query, PageQuery pageQuery) {
        return enrollmentService.getEnrollClassPage(query, pageQuery, StudentEnrollClassResp.class);
    }

    public List<StudentClassInfoResp> getStudentClassList(ParentStudentClassQuery query) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(
                        CLASS_INFO.DEFAULT_COLUMNS
//                        CLASS_STUDENT.DEFAULT_COLUMNS
                )
                .from(CLASS_STUDENT)
                    .join(CLASS_INFO).on(CLASS_STUDENT.CLASS_ID.eq(CLASS_INFO.ID))
                .where(CLASS_STUDENT.STUDENT_ID.eq(query.getStudentId()))
                    .and(CLASS_STUDENT.STUDENT_CLASS_STATUS.eq(StudentClassEnum.NORMAL))
                    .and(CLASS_INFO.CLASS_STATUS.in(ClassStatusEnum.WAITING, ClassStatusEnum.TEACHING));
        return classStudentService.getMapper().selectListWithRelationsByQueryAs(queryWrapper, StudentClassInfoResp.class);
    }

    public void changeClassOrLesson(ClassChangeReq classChangeReq) {
        classChangeService.createClassChangeRecord(classChangeReq);
    }

    public Page<StudentClassChangeRecordResp> getChangeRecordPage(StudentClassChangeRecordQuery query, PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(ENROLL_CHANGE_RECORD.DEFAULT_COLUMNS)
                .from(ENROLL_CHANGE_RECORD)
                .where(ENROLL_CHANGE_RECORD.STUDENT_ID.eq(query.getStudentId()))
                .and(ENROLL_CHANGE_RECORD.CHANGE_TYPE.in(ChangeTypeEnum.CHANGE_CLASS, ChangeTypeEnum.CHANGE_LESSON))
                .orderBy(ENROLL_CHANGE_RECORD.ID.desc());
       return enrollChangeRecordService.getMapper().paginateWithRelationsAs(pageQuery.toPage(), queryWrapper, StudentClassChangeRecordResp.class);
    }

    public List<StudentClassLessonResp> getClassLessonList(String classId) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(
                        LESSON.DEFAULT_COLUMNS,
                        LessonService.weekNameColumn().as(StudentClassLessonResp::getWeekName)
                )
                .from(LESSON)
                .where(LESSON.CLASS_ID.eq(classId))
                .orderBy(LESSON.LESSON_INDEX.asc());
        return lessonService.listAs(queryWrapper, StudentClassLessonResp.class);
    }
}
